//
//  KWTMGRNSettingSwitchCell.m
//  RuiHeApp
//
//  Created by Yan Chen on 2024/8/2.
//

#import "KWTMGRNSettingSwitchCell.h"
#import "ZYSwitch.h"

@interface KWTMGRNSettingSwitchCell()
@property (nonatomic, strong) QMUILabel *titleLab;

@property (nonatomic, strong) ZYSwitch *sw;

@property (nonatomic, strong) UIView *line;
@end
@implementation KWTMGRNSettingSwitchCell

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupUI];
    }
    return self;
}

-(void)setModel:(KWTMGRNSettingModel *)model
{
    _model = model;
    self.titleLab.text = model.rowTitle.rh_localized;
    self.sw.on = [model.content integerValue];
    self.sw.enabled = !model.readOnly;
}

-(void)setupUI
{
    kWeakSelf;
    self.contentView.backgroundColor = kCOLOR_HEX(@"#F0F5FF");
    UIView *bgView = [[UIView alloc]init];
    bgView.backgroundColor = UIColor.whiteColor;
    [self.contentView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0, 15, 0, 15));
    }];
    
    QMUILabel *titleLab = [QMUILabel labelWithText:@"" font:kSYSTEMFONT(14) textColor:kCOLOR_HEX_A(@"#171A1D",0.6)];
    titleLab.textAlignment = NSTextAlignmentLeft;
    [bgView addSubview:titleLab];
    self.titleLab = titleLab;
    
    
    _sw = [[ZYSwitch alloc]initWithFrame:CGRectMake(0, 0, 44, 24)];
    _sw.onTintColor = kCOLOR_HEX(@"#405DBE");
    _sw.on = NO;
    [_sw addTarget:self action:@selector(valueDidChange:) forControlEvents:UIControlEventValueChanged];
    [bgView addSubview:_sw];
    [_sw mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(@(CGSizeMake(44, 24)));
        make.right.mas_offset(-15);
        make.centerY.mas_offset(0);
    }];
    
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(15);
        make.left.mas_offset(20);
        make.right.equalTo(weakSelf.sw.mas_left).offset(-15);
        make.bottom.mas_offset(-15);
    }];
    
//    UIView *line = [[UIView alloc]init];
//    [bgView addSubview:line];
//    self.line = line;
//    line.backgroundColor = kCOLOR_HEX_A(@"#7E868E",0.2);
//    [line mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_offset(15);
//        make.right.mas_offset(-15);
//        make.height.offset(1);
//        make.bottom.mas_offset(0);
//    }];
    
}

-(void)showLine:(BOOL)state
{
    self.line.hidden = !state;
}

-(void)valueDidChange:(ZYSwitch*)sw
{
    self.model.content = [NSString stringWithFormat:@"%ld",(NSInteger)sw.isOn];
}

@end
